<?php
load_info_var($cf_controller);	   //加载smarty变量
load_file('order_class', LIB_PATH);
$main = new Order($db);
$id = isset($_POST['id']) ? intval($_POST['id']) : intval($_GET['id']);
$self = isset($_POST['self']) ? trim($_POST['self']) : trim($_GET['self']);
$return_page = $cf_pre_page ? $cf_pre_page : $cf_self_page;

require_once(INFO_PATH.'cf_var_cate.php');
load_file('cate_class', LIB_PATH);
switch ($cf_action) {
    case "index":	
	$smarty->mypage($cf_controller, $cf_action, 0);
	$stype = isset($_POST['stype']) ? trim($_POST['stype']) : trim($_GET['stype']);
	$stype2 = isset($_POST['stype2']) ? trim($_POST['stype2']) : trim($_GET['stype2']);
	$skey = isset($_POST['skey']) ? trim($_POST['skey']) : urldecode($_GET['skey']);
	$skey2 = isset($_POST['skey2']) ? trim($_POST['skey2']) : urldecode($_GET['skey2']);
	$skey3 = isset($_POST['skey3']) ? trim($_POST['skey3']) : urldecode($_GET['skey3']);
    $skey4 = isset($_POST['skey4']) ? trim($_POST['skey4']) : urldecode($_GET['skey4']);
    $skey5 = isset($_POST['skey5']) ? trim($_POST['skey5']) : urldecode($_GET['skey5']);
	$skey6 = isset($_POST['skey6']) ? trim($_POST['skey6']) : urldecode($_GET['skey6']);
	$where = '1';
	if ($skey) {
	     $where .= " and order_sn = '" . $skey . "' ";
	}

    if ($skey2) {		
		$where .= " and order_goods_title like '%" . $skey2 . "%'";
    }
	
	if ($skey3) {		
		$where .= " and order_name like '%" . $skey3 . "%'";
    }
	
    if($skey4 && $skey5){
        $where .= " and DATE_FORMAT(order_create_time,'%Y-%m-%d') between '".$skey4."' and '".$skey5."' ";
    }
	
	if ($skey6) {		
		$where .= " and order_user like '%" . $skey6 . "%'";
    }
	
	if (is_numeric($self)) {
	    $where .= " and order_status = '" . $self . "'";
	}
	
	if($self == '1' || $self == '2'){
		$order = "order_return_time DESC";
	}else{
		$order = "order_id DESC";	
	}
   
	load_file('page_class', LIB_PATH);
	$MyPage = new Mypage($db);
	$field = '*';
	$data_ary = $MyPage->from_sql($field, $main->get_table_name(), $where, $order, intval($_GET['cpage']), "30");
	$page = $MyPage->get_page($data, $cf_self_page . '?dpt=' . $cf_dpt . '&skey=' . $skey . '&skey2=' . $skey2  .'&skey3=' . $skey3 .'&skey4=' . $skey4 .'&skey5=' . $skey5 .'&skey6=' . $skey6 .   '&self=' . $self);
	// var_dump($data_ary);die;
	if($data_ary){
		foreach($data_ary as $k=>$v){
			$data_ary[$k]['order_detail'] = unserialize(stripslashes($v['order_detail']));	
		}
	}
	//echo $MyPage->get_sql();
	//dump($data_ary);	
	break; 
  
    case "del":
	$result = $main->del_data("order_id='$id'");
	if ($result) {
	    my_jump('删除成功', '', $return_page, 1);
	} else {
	    my_jump('删除失败!');
	}

	break;
	case "detail":
		$smarty->mypage($cf_controller, $cf_action, 0);
		load_file('order_class', LIB_PATH);
		$order = new Order($db);
		$row = $order->get_data_row($id);//订单信息
		if($row['order_detail'])
		$row['order_detail'] = unserialize(stripslashes($row['order_detail']));
		if($row['order_express_company'] && $row['order_express_number']){
			$row['express_info'] = $main->get_express($row['order_express_company'],$row['order_express_number']);		
		}
		
		//dump($row);
	break;
	case "send"://订单发货操作
		
		$data['order_express_company'] = $_POST['order_express_company']; 
		$data['order_express_company_name'] = $cf_info_var_common['express_company'][$_POST['order_express_company']];
		$data['order_express_number'] = $_POST['order_express_number']; 
		$data['order_express_remark'] = $_POST['order_express_remark']; 
		$data['order_status'] = '2';		
		$data['order_express_time'] = date("Y-m-d H:i:s");
		$data['order_express_operator'] = $cf_admin_info['admin_name'];	
			
		$result = $main->update_data($data, "order_id='".$id."'");
	 if ($result)
		my_jump('操作成功', '', $return_page, 1);
	    else
		my_jump('操作失败');
	break;	
   	case "complete"://订单完成操作			
		$result = $main->complete_order($id);
	    if ($result=='1') {
			my_jump('操作成功', '', $return_page, 1);
	    }elseif($result=='2'){
			my_jump('操作失败');
		}else {
			my_jump('没有此订单信息!');
	    }		
	break;
	case "close"://订单关闭操作	
		$data['order_close_reason'] = $_POST['order_close_reason'];
		$data['order_close_time'] = date("Y-m-d H:i:s");		
		$data['order_status'] = '4';		
		$data['order_close_operator'] = $cf_admin_info['admin_name'];						
		$result = $main->update_data($data, "order_id = '$id'");
	    if ($result) {
			my_jump('操作成功', '', $return_page, 2);
	    } else {
			my_jump('操作失败!');
	    }
	break;
	case "repay"://订单退款操作	
		$data['order_repay_reason'] = $_POST['order_repay_reason'];
		$data['order_repay_sn'] = $_POST['order_repay_sn'];
		$data['order_repay_time'] = date("Y-m-d H:i:s");		
		$data['order_status'] = '5';		
		$data['order_repay_operator'] = $cf_admin_info['admin_name'];						
		$result = $main->update_data($data, "order_id = '$id'");
	    if ($result) {
			my_jump('操作成功', '', $return_page, 2);
	    } else {
			my_jump('操作失败!');
	    }
	break;
    
    default:
	my_jump('非法操作!');
	break;
}

$smarty->assign(array(
    'id' => $id,
    'row' => $row,
    'data_ary' => $data_ary,
    'sessionid' => session_id(),
    'page' => $page,
    'self' => $self,
    'skey' => $skey,
    'skey2' => $skey2,
    'skey3' => $skey3,
    'skey4' => $skey4,
    'skey5' => $skey5,
	'skey6' => $skey6, 
    'group' => $group,
    'result' => $result,
    'sum' => $sum,
	'real_count' => $real_count,
	'total_count' => $total_count ,
	'cf_var_cate' => $cf_var_cate    
));


$smarty->mydisplay();
?>